class_name ShopPanel
extends Panel

signal on_shop_next_wave

const SHOP_CARD_SCENE = preload("uid://b6u8w5f87iuc3")

@export var shop_items: Array[ItemBase]


@onready var items_container: HBoxContainer = %ItemsContainer
@onready var passives_container: GridContainer = %PassivesContainer
@onready var weapons_container: GridContainer = %WeaponsContainer
@onready var combine_button: Button = %CombineButton

var context_card: ItemCard

func load_shop(current_wave: int) -> void:
	context_card = null
	var selected_items := Global.select_items_for_offer(shop_items, current_wave, Global.SHOP_PROBABILITY_CONFIG)
	var shop_card: ShopCard = null
	for i in selected_items.size():
		var current_child_count := items_container.get_child_count()
		if i >= current_child_count:
			shop_card = SHOP_CARD_SCENE.instantiate()
			items_container.add_child(shop_card)
		else:
			shop_card = items_container.get_child(i)
		if not shop_card.on_item_purchased.is_connected(_on_item_purchased):
			shop_card.on_item_purchased.connect(_on_item_purchased)
		shop_card.shop_item = selected_items[i]


func create_item_card() -> ItemCard:
	var item_card := Global.ITEM_CARD_SCENE.instantiate() as ItemCard
	item_card.on_item_card_selected.connect(_on_item_card_selected)
	return item_card
	
func create_item_weapon(weapon: ItemWeapon) -> void:
	var item_card := create_item_card()
	weapons_container.add_child(item_card)
	item_card.item = weapon


func _on_new_wave_button_pressed() -> void:
	SoundManager.player_sound(SoundManager.Sound.UI)
	on_shop_next_wave.emit()
	
func _on_item_card_selected(card: ItemCard) -> void:
	SoundManager.player_sound(SoundManager.Sound.UI)
	context_card = card
	var can_merge := false
	if card.item.item_type == ItemBase.ItemType.WEAPON:
		var count := 0
		for weapon in Global.equipped_weapons:
			if weapon.item_name == card.item.item_name:
				count += 1
		if count >= 2:
			can_merge = true
	combine_button.disabled = not can_merge

func _on_item_purchased(item: ItemBase) -> void:
	SoundManager.player_sound(SoundManager.Sound.UI)
	var item_card := create_item_card()
	if item.item_type == ItemBase.ItemType.WEAPON:
		weapons_container.add_child(item_card)
		Global.player.add_weapon(item)
	elif item.item_type == ItemBase.ItemType.PASSIVE:
		passives_container.add_child(item_card)
		(item as ItemPassive).apply_passive()
	item_card.item = item


func _on_combine_button_pressed() -> void:
	SoundManager.player_sound(SoundManager.Sound.UI)
	if not context_card:
		return
	var clicked_weapon := context_card.item as ItemWeapon
	if not clicked_weapon.upgrade_to:
		return
	var weapons_to_move := Global.player.current_weapons.filter(func(w: Weapon):return w.data.item_name == clicked_weapon.item_name).slice(0, 2)
	if weapons_to_move.size() < 2:
		return
	var cards_to_move := weapons_container.get_children().filter(func(c: ItemCard):return c.item.item_name == clicked_weapon.item_name).slice(0, 2)
	if cards_to_move.size() < 2:
		return
	for weapon: Weapon in weapons_to_move:
		Global.player.del_weapon(weapon)
		weapon.queue_free()
	for card: ItemCard in cards_to_move:
		card.queue_free()
	var upgrade_weapon := load(clicked_weapon.upgrade_to.resource_path)
	Global.player.add_weapon(upgrade_weapon)
	var new_card := create_item_card()
	weapons_container.add_child(new_card)
	new_card.item = upgrade_weapon
	context_card = null


func _on_sell_button_pressed() -> void:
	SoundManager.player_sound(SoundManager.Sound.UI)
	if not context_card:
		return
	var coins = min(1, floor(context_card.item.item_cost * 0.75))
	var clicked_weapon := context_card.item as ItemWeapon
	var weapon_to_move : Weapon = Global.player.current_weapons.filter(func(w: Weapon):return w.data.item_name == clicked_weapon.item_name).front()
	if weapon_to_move:
		Global.player.del_weapon(weapon_to_move)
		weapon_to_move.queue_free()
	context_card.queue_free()
	context_card = null
	Global.coins += coins
